package com.zzyl.nursing.controller.member;

import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.framework.config.properties.AliIoTConfigProperties;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.MemberBindDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.DeviceDataItem;
import com.zzyl.nursing.vo.FamilyMemberElderPageVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属的接口")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;


    @Autowired
    private Client client;

    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;

    @Autowired
    private IDeviceDataService deviceDataService;

    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto){
        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }

    @PostMapping("/add")
    @ApiOperation("绑定家人")
    public AjaxResult memberBinding(@RequestBody MemberBindDto dto){
        familyMemberService.memberBinding(dto);
        return AjaxResult.success();

    }

    @GetMapping("/my")
    @ApiOperation("查看家人列表")
    public AjaxResult myMember(Integer pageNum,Integer pageSize){
        startPage();
        List<FamilyMemberElderVo> familyMemberElderVos =  familyMemberService.myMember();
        return success(familyMemberElderVos);
    }

    @GetMapping("/list-by-page")
    @ApiOperation("查看家人列表分页查询")
    public AjaxResult listByPage(Integer pageNum,Integer pageSize){
        startPage();
        List<FamilyMemberElderPageVo> pageVoList = familyMemberService.listByPage();
        return success(pageVoList);
    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation("查询健康数据")
    public AjaxResult queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) throws Exception {
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        QueryDevicePropertyStatusResponse devicePropertyStatusResponse = client.queryDevicePropertyStatus(request);
        return  success(devicePropertyStatusResponse.getBody().getData());
    }

    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation("按天统计查询指标数据")
    public AjaxResult queryDeviceDataListByDay(@RequestParam String functionId,
                                               @RequestParam Long startTime,
                                               @RequestParam Long endTime,
                                               @RequestParam String iotId) throws Exception {
        List<DeviceDataItem<Double>> data =  deviceDataService.selectDeviceDataListVo(functionId,iotId,startTime,endTime);
        return success(data);
    }

    @GetMapping("/queryDeviceDataListByWeek")
    public AjaxResult queryDeviceDataListByWeek(@RequestParam String functionId,
                                                @RequestParam Long startTime,
                                                @RequestParam Long endTime,
                                                @RequestParam String iotId){
        List<DeviceDataItem<Integer>> data = deviceDataService.selectDeviceDataListVoWeek(functionId, iotId, startTime, endTime);
        return success(data);
    }


}
